/**
  ******************************************************************************
  * @file    : iic_eeprom.h
  * @author  : EndLess
  * @version : V1.0.0
  * @date    : 2018-4-3
  * @brief   : eeprom驱动代码
  *
  ******************************************************************************
  */
#ifndef __BOARD_H__
#define __BOARD_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f1xx_hal.h"

/**
 * @brief LED Types Definition
 */
typedef enum 
{
  LED1 = 0,
  LED2 = 1,
  LED3 = 2,
  LED4 = 3,

  LED_STAT  = LED1,
  LED_WORK = LED2,
  LED_NET    = LED3,
  LED_FAULT   = LED4

} Led_TypeDef;

#define LEDn 4

#define LED1_PIN GPIO_PIN_3 /* PD.07*/
#define LED1_GPIO_PORT GPIOC
#define LED1_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
#define LED1_GPIO_CLK_DISABLE() __HAL_RCC_GPIOC_CLK_DISABLE()

#define LED2_PIN GPIO_PIN_2 /* PD.13*/
#define LED2_GPIO_PORT GPIOC
#define LED2_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
#define LED2_GPIO_CLK_DISABLE() __HAL_RCC_GPIOC_CLK_DISABLE()

#define LED3_PIN GPIO_PIN_1 /* PD.03*/
#define LED3_GPIO_PORT GPIOC
#define LED3_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
#define LED3_GPIO_CLK_DISABLE() __HAL_RCC_GPIOC_CLK_DISABLE()

#define LED4_PIN GPIO_PIN_0 /* PD.04*/
#define LED4_GPIO_PORT GPIOC
#define LED4_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
#define LED4_GPIO_CLK_DISABLE() __HAL_RCC_GPIOC_CLK_DISABLE()

#define LEDx_GPIO_CLK_ENABLE(__LED__) \
    do                                \
    {                                 \
        if ((__LED__) == LED1)        \
            LED1_GPIO_CLK_ENABLE();   \
        else if ((__LED__) == LED2)   \
            LED2_GPIO_CLK_ENABLE();   \
        else if ((__LED__) == LED3)   \
            LED3_GPIO_CLK_ENABLE();   \
        else if ((__LED__) == LED4)   \
            LED4_GPIO_CLK_ENABLE();   \
    } while (0)

#define LEDx_GPIO_CLK_DISABLE(__LED__) (((__LED__) == LED1) ? LED1_GPIO_CLK_DISABLE() : \
                                        ((__LED__) == LED2) ? LED2_GPIO_CLK_DISABLE() : \
                                        ((__LED__) == LED3) ? LED3_GPIO_CLK_DISABLE() : \
                                        ((__LED__) == LED4) ? LED4_GPIO_CLK_DISABLE() : 0)

                                              
/**
 * @brief LED Types Definition
 */
typedef enum 
{
  PWR1 = 0,
  PWR2 = 1,
  PWR3 = 2,
  PWR4 = 3,

  PWR_GREEN  = PWR1,
  PWR_ORANGE = PWR2,
  PWR_RED    = PWR3,
  PWR_BLUE   = PWR4

} Pwr_TypeDef;

#define PWRn                             4

#define PWR1_PIN                         GPIO_PIN_7             /* PD.07*/
#define PWR1_GPIO_PORT                   GPIOC
#define PWR1_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOC_CLK_ENABLE()
#define PWR1_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOC_CLK_DISABLE()
  
#define PWR2_PIN                         GPIO_PIN_8            /* PD.13*/
#define PWR2_GPIO_PORT                   GPIOC
#define PWR2_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOC_CLK_ENABLE()
#define PWR2_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOC_CLK_DISABLE()


#define PWR3_PIN                         GPIO_PIN_9            /* PD.03*/
#define PWR3_GPIO_PORT                   GPIOC
#define PWR3_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOC_CLK_ENABLE()
#define PWR3_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOC_CLK_DISABLE()


#define PWR4_PIN                         GPIO_PIN_8            /* PD.04*/
#define PWR4_GPIO_PORT                   GPIOA
#define PWR4_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOA_CLK_ENABLE()
#define PWR4_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOA_CLK_DISABLE()

#define PWRx_GPIO_CLK_ENABLE(__PWR__)    do { if ((__PWR__) == PWR1) PWR1_GPIO_CLK_ENABLE(); else \
                                              if ((__PWR__) == PWR2) PWR2_GPIO_CLK_ENABLE(); else \
                                              if ((__PWR__) == PWR3) PWR3_GPIO_CLK_ENABLE(); else \
                                              if ((__PWR__) == PWR4) PWR4_GPIO_CLK_ENABLE();} while(0)

#define PWRx_GPIO_CLK_DISABLE(__PWR__)   (((__PWR__) == PWR1) ? PWR1_GPIO_CLK_DISABLE() :\
                                          ((__PWR__) == PWR2) ? PWR2_GPIO_CLK_DISABLE() :\
                                          ((__PWR__) == PWR3) ? PWR3_GPIO_CLK_DISABLE() :\
                                          ((__PWR__) == PWR4) ? PWR4_GPIO_CLK_DISABLE() : 0 )

                     
/**
 * @brief BUTTON Types Definition
 */
typedef enum 
{
  BUTTON_WAKEUP = 0,
  BUTTON_TAMPER = 1,
  BUTTON_KEY    = 2,
  BUTTON_DOOR   = 3

} Button_TypeDef;

typedef enum 
{  
  BUTTON_MODE_GPIO = 0,
  BUTTON_MODE_EXTI = 1
} ButtonMode_TypeDef;

#define BUTTONn                          4

/**
 * @brief Tamper push-button
 */
#define TAMPER_BUTTON_PIN                   GPIO_PIN_13             /* PC.13*/
#define TAMPER_BUTTON_GPIO_PORT             GPIOC
#define TAMPER_BUTTON_GPIO_CLK_ENABLE()     __HAL_RCC_GPIOC_CLK_ENABLE()
#define TAMPER_BUTTON_GPIO_CLK_DISABLE()    __HAL_RCC_GPIOC_CLK_DISABLE()
#define TAMPER_BUTTON_EXTI_IRQn             EXTI15_10_IRQn

/**
 * @brief Key push-button
 */
#define KEY_BUTTON_PIN                      GPIO_PIN_1             /* PB.09*/
#define KEY_BUTTON_GPIO_PORT                GPIOA
#define KEY_BUTTON_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOA_CLK_ENABLE()
#define KEY_BUTTON_GPIO_CLK_DISABLE()       __HAL_RCC_GPIOA_CLK_DISABLE()
#define KEY_BUTTON_EXTI_IRQn                EXTI9_5_IRQn

/**
 * @brief Wake-up push-button
 */
#define WAKEUP_BUTTON_PIN                   GPIO_PIN_0             /* PA.00*/
#define WAKEUP_BUTTON_GPIO_PORT             GPIOA
#define WAKEUP_BUTTON_GPIO_CLK_ENABLE()     __HAL_RCC_GPIOA_CLK_ENABLE()
#define WAKEUP_BUTTON_GPIO_CLK_DISABLE()    __HAL_RCC_GPIOA_CLK_DISABLE()
#define WAKEUP_BUTTON_EXTI_IRQn             EXTI0_IRQn

/**
 * @brief Key push-button
 */
#define DOOR_BUTTON_PIN                      GPIO_PIN_11             /* PB.09*/
#define DOOR_BUTTON_GPIO_PORT                GPIOA
#define DOOR_BUTTON_GPIO_CLK_ENABLE()        __HAL_RCC_GPIOA_CLK_ENABLE()
#define DOOR_BUTTON_GPIO_CLK_DISABLE()       __HAL_RCC_GPIOA_CLK_DISABLE()
#define DOOR_BUTTON_EXTI_IRQn                EXTI15_10_IRQn

#define BUTTONx_GPIO_CLK_ENABLE(__BUTTON__) do { if ((__BUTTON__) == BUTTON_TAMPER) TAMPER_BUTTON_GPIO_CLK_ENABLE()  ; else \
                                                 if ((__BUTTON__) == BUTTON_KEY) KEY_BUTTON_GPIO_CLK_ENABLE()  ; else \
                                                 if ((__BUTTON__) == BUTTON_DOOR) WAKEUP_BUTTON_GPIO_CLK_ENABLE()  ; else \
                                                 if ((__BUTTON__) == BUTTON_WAKEUP) WAKEUP_BUTTON_GPIO_CLK_ENABLE();} while(0)

#define BUTTONx_GPIO_CLK_DISABLE(__BUTTON__)    (((__BUTTON__) == BUTTON_TAMPER) TAMPER_BUTTON_GPIO_CLK_DISABLE()  :\
                                                 ((__BUTTON__) == BUTTON_KEY) KEY_BUTTON_GPIO_CLK_DISABLE()  :\
                                                 ((__BUTTON__) == BUTTON_DOOR) DOOR_BUTTON_GPIO_CLK_DISABLE()  :\
                                                 ((__BUTTON__) == BUTTON_WAKEUP) WAKEUP_BUTTON_GPIO_CLK_DISABLE()  : 0 )                                             
                                              
                                              
/**
 * @brief LED Types Definition
 */
typedef enum 
{
  RS4851 = 0,
  RS4852 = 1,
  RS4853 = 2,
  RS4854 = 3,

  RS485_SOLAR  = RS4851,

} Rs485_TypeDef;

#define RS485n                             4

#define RS4851_PIN                         GPIO_PIN_15             /* PD.07*/
#define RS4851_GPIO_PORT                   GPIOA
#define RS4851_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOA_CLK_ENABLE()
#define RS4851_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOA_CLK_DISABLE()
  
#define RS4852_PIN                         GPIO_PIN_3            /* PD.13*/
#define RS4852_GPIO_PORT                   GPIOB
#define RS4852_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOB_CLK_ENABLE()
#define RS4852_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOB_CLK_DISABLE()


#define RS4853_PIN                         GPIO_PIN_4            /* PD.03*/
#define RS4853_GPIO_PORT                   GPIOB
#define RS4853_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOB_CLK_ENABLE()
#define RS4853_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOB_CLK_DISABLE()


#define RS4854_PIN                         GPIO_PIN_5            /* PD.04*/
#define RS4854_GPIO_PORT                   GPIOB
#define RS4854_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOB_CLK_ENABLE()
#define RS4854_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOB_CLK_DISABLE()

#define RS485x_GPIO_CLK_ENABLE(__RS485__)    do { if ((__RS485__) == RS4851) RS4851_GPIO_CLK_ENABLE(); else \
                                              if ((__RS485__) == RS4852) RS4852_GPIO_CLK_ENABLE(); else \
                                              if ((__RS485__) == RS4853) RS4853_GPIO_CLK_ENABLE(); else \
                                              if ((__RS485__) == RS4854) RS4854_GPIO_CLK_ENABLE();} while(0)

#define RS485x_GPIO_CLK_DISABLE(__RS485__)   (((__RS485__) == RS4851) ? RS4851_GPIO_CLK_DISABLE() :\
                                          ((__RS485__) == RS4852) ? RS4852_GPIO_CLK_DISABLE() :\
                                          ((__RS485__) == RS4853) ? RS4853_GPIO_CLK_DISABLE() :\
                                          ((__RS485__) == RS4854) ? RS4854_GPIO_CLK_DISABLE() : 0 )


#define WDG_PIN GPIO_PIN_0 /* PA.00*/
#define WDG_GPIO_PORT GPIOA
#define WDG_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define WDG_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE()


void BSP_LED_Init(Led_TypeDef Led);
void BSP_LED_On(Led_TypeDef Led);
void BSP_LED_Off(Led_TypeDef Led);
void BSP_LED_Toggle(Led_TypeDef Led);
void BSP_PWR_Init(Pwr_TypeDef Pwr);
void BSP_PWR_On(Pwr_TypeDef Pwr);
void BSP_PWR_Off(Pwr_TypeDef Pwr);
void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Button_Mode);
uint32_t BSP_PB_GetState(Button_TypeDef Button);
void BSP_WDG_Init(void);
void BSP_WDG_Feed(void);    
#ifdef __cplusplus
    }
#endif
#endif /* __BOARD_H__ */
/*****************************END OF FILE**************************************/
